package com.biscuit.page.basicservice.service;

import com.biscuit.page.basicservice.persistence.dao.AiChatDialogueDao;
import com.biscuit.page.basicservice.persistence.dao.AiChatDialogueRebuildDao;
import com.biscuit.page.basicservice.websocket.ai_chat.AiChatSessionWebSocketContext;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChunk;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.service.ArkService;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author biscuit
 * @create 2024年07月31日 16:19
 */
@Slf4j
@Service
public class VolcanoService {

    @Resource
    private AiChatDialogueDao aiChatDialogueDao;

    @Resource
    private AiChatDialogueRebuildDao aiChatDialogueRebuildDao;

    private final String ARK_API_KEY = "16189e3c-053d-4c98-8287-7d53ad34b5fc";

    private final String ENDPOINT_ID = "ep-20240731163738-7msqf";

    private final String BOT_ID = "bot-20240731173216-2nx2d";

    public void asyncAiChat(List<ChatMessage> chatMessageList, AiChatSessionWebSocketContext context) {
        // 构建调用对象
        ArkService service = ArkService.builder()
                .apiKey(ARK_API_KEY)
                .baseUrl("https://ark.cn-beijing.volces.com/api/v3/")
                .build();

        // 对话请求对象
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(ENDPOINT_ID)
                .messages(chatMessageList)
                .build();

        // 对话结果接收对象
        StringBuilder result = StringUtils.isNotBlank(context.getResult()) ? context.getResult() : new StringBuilder();

        // 发起对话请求
        // Flowable<ChatCompletionChunk> chatCompletionChunkFlowable = service.streamChatCompletion(chatCompletionRequest);
        // ChatCompletionResult chatCompletion = service.createChatCompletion(chatCompletionRequest);
        Flowable<ChatCompletionChunk> flowable = service.streamChatCompletion(chatCompletionRequest);
        context.setFlowable(flowable);

        // 流式处理对话响应
        Disposable subscribe = flowable.subscribe(
                // 监听每次返回的部分结果
                chatCompletionChunk -> {
                    // 获取本次响应内容
                    String content = chatCompletionChunk.getChoices().get(0).getMessage().getContent().toString();

                    // 汇总单次响应内容
                    result.append(content);

                    // 将本次响应内容写回页面
                },
                // 发送错误回调
                throwable -> {
                    // 发送错误时，将上下文对象重置
                    context.setResult(null);
                    context.setFlowable(null);
                },
                // 处理完成回调
                () -> {
                    // 完成处理时，将上下文对象重置
                    context.setResult(null);
                    context.setFlowable(null);
                }
        );

        // 获取对话结果
        // List<ChatCompletionChoice> choices = chatCompletion.getChoices();

        // 返回对话结果
        // return choices.get(0).getMessage().getContent().toString();
    }

}
